package cn.azzhu.service;

import cn.azzhu.bean.Order;
import cn.azzhu.bean.Product;
import cn.azzhu.mapper.ProductMapper;
import cn.azzhu.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author azzhu
 * @create 2020-08-14 09:48:20
 */
public class ProductService {

    public PageInfo<Product> getPage(int pageNum,int pageSize){
        //1.在sql语句执行之前，将分页参数设置进去
        PageHelper.startPage(pageNum,pageSize);

        //2.查询所有
        SqlSession sqlSession = MyBatisUtil.getSqlSession();

        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);

        List<Product> products = mapper.getProducts();

        //3.需要构建一个pageInfo
        final PageInfo<Product> pageInfo = new PageInfo<>(products);

        MyBatisUtil.closeSqlSession();
        return pageInfo;
    }
}
